﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;

namespace WXYW
{
    public partial class SearchProductPictures : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void BindApplicationForm()
        {
            ApplicationService service = new ApplicationService();
            ApplicationForm applicationForm = service.GetApplicationFormByRMACode(RMAIDtxtBox.Text.Trim());
            if (applicationForm == null)
            {
                msgLabel.Visible = true;
                return;
            }
            else
            {
                msgLabel.Visible = false;
                ClientScript.RegisterStartupScript(this.GetType(), "show", "document.getElementById('mainForm').style.display='inline';", true);
            }

            //ApplicationForm applicationForm = service.GetApplicationFormDetailByID(original.ApplicationID);
            if (!applicationForm.UserReference.IsLoaded)
            {
                applicationForm.UserReference.Load();
            }
            ViewState["Type"] = applicationForm.ApplicationType;
            rmaCodeLabel.Text = applicationForm.ApplicationCode;
            applyTimeLabel.Text = applicationForm.ApplicationDateTime.HasValue ? applicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd") : "";
            receiveTimeLabel.Text = applicationForm.ReceiveDateTime.HasValue ? applicationForm.ReceiveDateTime.Value.ToString("yyyy-MM-dd") : "";
            distributeTimeLabel.Text = applicationForm.DistributeDateTime.HasValue ? applicationForm.DistributeDateTime.Value.ToString("yyyy-MM-dd") : "";

            applyCompanyNameTxtBox.Text = applicationForm.User.FullName;
            mailAddressTxtBox.Text = applicationForm.User.UserAddress;
            applierEmailTxtBox.Text = applicationForm.User.UserEmail;
            applierFaxTxtBox.Text = applicationForm.User.UserFax;
            statusLbl.Text = applicationForm.Status;
            applierMobilePhoneTxtBox.Text = applicationForm.User.UserMobilePhone;
            applierNameTxtBox.Text = applicationForm.User.ContactPerson;
            applierPhoneTxtBox.Text = applicationForm.User.UserPhone;
            ProviderNameTxtBox.Text = applicationForm.ProviderName;

            if (!applicationForm.ApplicationPicture.IsLoaded)
            {
                applicationForm.ApplicationPicture.Load();
            }
            List<ApplicationPicture> picturelist = applicationForm.ApplicationPicture.ToList();
            Table table = new Table();
            table.Rows.Add(new TableRow());
            table.Rows.Add(new TableRow());

            Table table2 = new Table();
            table2.Rows.Add(new TableRow());
            table2.Rows.Add(new TableRow());
            foreach (ApplicationPicture picture in picturelist)
            {
                TableCell celllImage = new TableCell();
                Image imageControl = new Image();
                imageControl.Height = 100;
                imageControl.ImageUrl = "Picture/" + picture.PictureNameOnServer;
                imageControl.Attributes.Add("OnClick", "window.open('ImageDetail.aspx?Name=" + picture.PictureNameOnServer + "')");
                celllImage.Controls.Add(imageControl);

                TableCell cellName = new TableCell();
                Label label = new Label();
                label.Text = picture.PictureName;
                cellName.Controls.Add(label);

                if (picture.Type == (int)PictureType.MaintainProduct)
                {
                    table.Rows[0].Cells.Add(celllImage);
                    table.Rows[1].Cells.Add(cellName);
                }
                else
                {
                    table2.Rows[0].Cells.Add(celllImage);
                    table2.Rows[1].Cells.Add(cellName);
                }
            }
            picturePanel.Controls.Add(table);
            picturePanel2.Controls.Add(table2);
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            BindApplicationForm();
        }
    }
}
